package com.zygame.sdk.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zygame.sdk.bean.UserBean;

import java.util.List;

public class UserDao extends BaseDao<UserBean> {

	private final String _TABLE = "T_ZYUSER";
	private final String _ID = "_ID";
	public final String _USERNAME = "_USERNAME";
	public final String _PWD = "_PWD";
	public final String _LAST_LOGIN_FLAG = "_LAST_LOGIN_FLAG";
	public final String _TYPE = "_TYPE";
	public final String _OTHER= "_OTHER";

	public String userName;
	public String passWord;

	@Override
	protected void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		sql.append(" CREATE TABLE IF NOT EXISTS ").append(getTableName())
		.append(" ( ").append(_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT ").append(" , ")
		.append(_USERNAME).append(VARCHAR).append(" , ")
		.append(_PWD).append(VARCHAR).append(" , ")
		.append(_LAST_LOGIN_FLAG).append(VARCHAR).append(" , ")
		.append(_OTHER).append(VARCHAR).append(" , ")
		.append(_TYPE).append(VARCHAR)
		.append(" ) ");

		db.execSQL(sql.toString());

	}

	@Override
	protected void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	@Override
	protected UserBean cursor2Model(Cursor cursor) {
		UserBean userBean = new UserBean(cursor.getString(cursor.getColumnIndex(UserDao.this._USERNAME)),
				cursor.getString(cursor.getColumnIndex(UserDao.this._PWD)),
				cursor.getString(cursor.getColumnIndex(UserDao.this._TYPE)),
				cursor.getString(cursor.getColumnIndex(UserDao.this._OTHER)),
				cursor.getString(cursor.getColumnIndex(UserDao.this._LAST_LOGIN_FLAG)));

		return userBean;
	}

	@Override
	protected String getTableName() {
		// TODO Auto-generated method stub
		return _TABLE;
	}

	@Override
	protected ContentValues buildContentValues(UserBean userBean) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
//		values.put(_ID, value)		
		values.put(_USERNAME, userBean.userName);
		values.put(_PWD, userBean.pwd);
		values.put(_LAST_LOGIN_FLAG, userBean.lastLoginFlag);
		values.put(_TYPE, userBean.type);
		values.put(_OTHER, userBean.other);

		return values;
	}

	/**  
	 * 根据id(物理)删除
	 * 
	 * @param id
	 */  
	public int del(String str) {
		// String sql = " DELETE FROM " + getTableName() + " WHERE _ID=" + id;
		// this.getDatabase().execSQL(sql);

		return this.getDatabase().delete(getTableName(), _USERNAME + "=?", new String[] { str + "" });
	}  
	
	/**
	 * 添加进数据库
	 * @param ub
	 */
	public void addToDb(UserBean ub){
		/*
		 *   每次插入，先把_LAST_LOGIN_FLAG这一列设置为0，在插入数据
		 */
		UserDao dao2 = new UserDao();
		String sql = null;
		
		sql = "update " + _TABLE + " set " + _LAST_LOGIN_FLAG + " =0 ";
		dao2.getDatabase().execSQL(sql);
		
		try {
			if(dao2.query(dao2._USERNAME, ub.userName) == null){
				dao2.add(ub);
			}else{
//				sql = "update " + _TABLE + " set " + _LAST_LOGIN_FLAG + "=1,_PWD=" + ub.pwd + " where " + dao2._USERNAME + "=" + "\"" + ub.userName + "\"";
//				sql = "update " + _TABLE + " set " + _LAST_LOGIN_FLAG + "=1,_PWD=? where " + dao2._USERNAME + "=?";
////				this.getDatabase().update(table, values, whereClause, whereArgs)
//				String [] strs= {ub.pwd,ub.userName};
//				dao2.getDatabase().execSQL(sql, strs);
//				dao2.update(ub, dao2._USERNAME, new String[]{ub.userName});
				ub.lastLoginFlag = "1";
				dao2.getDatabase().update(_TABLE, this.buildContentValues(ub), dao2._USERNAME +"= ?", new String[]{ub.userName});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void out(){
		UserDao dao2 = new UserDao();
		String sql = null;

		sql = "update " + _TABLE + " set " + _LAST_LOGIN_FLAG + " =0 ";
		dao2.getDatabase().execSQL(sql);
	}

	@Override
	public List<UserBean> queryList() {
		String sql = " SELECT * FROM " + getTableName() + " WHERE "+_TYPE+"=1";
		return queryList(sql);
	}



	public UserBean queryBean() {
//		String sql = " SELECT * FROM " + getTableName() + " WHERE " + rowName + "=" + "\"" + rowValue + "\"";
		String sql = " SELECT * FROM " + getTableName() + " WHERE " + _LAST_LOGIN_FLAG + "= '1'" +" AND "+_TYPE+"='1' " ;
		UserBean t = null;
		try {
			Cursor cursor = this.getDatabase().rawQuery(sql, null);
			while (cursor.moveToFirst()) {
				t = this.cursor2Model(cursor);
				break;
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}
}
